<template>
  <div class="container">
    <div class="search-box">
      <van-search
        :value='keyword'
        placeholder="点击输入搜索内容"
        use-action-slot
        shape="round"
        @change="onChangeVal"
        @search="onSearch"
        :autofocus="true"
        ref="searchBox"
      >
        <div class="search-btn" size="small" slot="action" @tap="onSearch2">搜索</div>
      </van-search>
    </div>
    <div class="height-39"></div>
    <div class="pull-refresh pad-4">
      <div v-for="(item, index) in orders" :key="index">
        <div class="order-div">
          <div class="flex mar-b-10">
            <img class="userinfo-avatar" :src="item.customerAvatar" mode="cover"/>
            <label class="flex-item">{{item.customerNickname}}</label>
            <div v-if="item.orderStatus === 0" class="font-orange">待付款</div>
            <div v-if="item.orderStatus === 1" class="font-orange">待发货</div>
            <div v-if="item.orderStatus === 2" class="font-orange">待收货</div>
            <div v-if="item.orderStatus === 3" class="font-orange">已收货</div>
            <div v-if="item.orderStatus === 4" class="font-orange">已完成</div>
          </div>
          <div v-for="(itemList, indexList) in item.orderGoodsList" :key="indexList" @click="queryOrderInfo(item)">
            <div class="flex font-10 v-align-c border-t pad-t-8 pad-b-8">
              <div>
                <img :src="itemList.goodsImageUrl" class="width-66 height-66 mar-r-10 border-radius-5" />
              </div>
              <div class="flex-item">
                <div class="font-14 title-line mar-b-10">{{itemList.goodsTitle}}</div>
                <div class="font-12 flex v-align-c">
                  <span class="tag-win" v-if="itemList.memberCommission">收益￥{{itemList.memberCommission}}</span>
                  <span class="flex-item align-r">x{{itemList.goodsNum}}</span>
                </div>
              </div>            
            </div>
            <div class="align-r font-12 pad-b-8">
              <span v-if="item.orderGoodsList.length > 1" class="font-15 mar-l-5 font-red">￥{{itemList.goodsPrice}}</span>
              <span v-if="item.orderGoodsList.length == 1" class="font-15 mar-l-5 font-red">￥{{item.actualPrice}}</span>
            </div>
          </div>
          <div v-if="item.moreGoods" class="flex flex-center font-12 pad-t-10" @click="queryOrderInfo(item)">查看更多 ></div>
          <div class="pad-l-15 pad-t-19 border-t font-14 flex">
            <div class="flex-item font-gray"> &nbsp; </div>
            <div>              
              <span class="order-btn" @click="queryOrderInfo(item)">查看详情</span>
              <span v-if="item.orderStatus===2" class="order-btn mar-l-10" @click="queryExpress(item)">查看物流</span>
              <span v-if="item.orderStatus===3" class="order-btn mar-l-10" @click="queryExpress(item)">查看物流</span>
            </div>
          </div>
        </div>
      </div>
      <div class="pad-10 mar-t-10 align-c" v-if="orders.length===0">
        <img v-if="fileSpace" mode="widthFix" class="width-153 height-75 mar-t-30"
             :src="fileSpace + '/images/2-0-kong.png'"/>
        <div class="pad-t-20 font-14 font-gray">空空如也</div>
      </div>
    </div>
    <div class="pad-10"></div>
  </div>
</template>
<script>
import {
  pageCustomerOrder,
  pageUserOrder,
  cancelOrder,
  checkCancelPaidOrder
} from '@/api/personal'
import {
  pageProjectOrderByMemberId
} from '@/api/distribution'
import { getDict } from '@/api/common'
export default {
  data() {
    return {
      keyword: '',
      currentPage: 1,
      pullLoad: {
        loading: false,
        finished: false
      },
      orders: [],
      searchNull: false,
      orderStatusMap: {},
      searchType: ''
    }
  },
  onLoad() {
    // 初始化data
    Object.assign(this.$data, this.$options.data())
  },
  mounted() {
    this.searchType = this.getCurrentPage().query.searchType
    this.getOrderDic()
  },
  onReachBottom() {
    // 触底刷新
    if (!this.pullLoad.finished) {
      this.pageUserOrder()
    }
  },
  methods: {
    goShop(memberId) {
      this.$utils.setMyId(memberId)
      this.$root.$mp.appOptions.query = {}
      this.$pt.reLaunch({ url: '/pages/shop/main' })
    },
    cancelOrder(item) {
      // 取消订单事件
      // 订单里面都没有发货才可以取消订单，到订单取消页面
      // 否则提示联系卖家处理
      if (item.orderStatus === 0) {
        const that = this
        this.$pt.showModal({
          title: '温馨提示',
          content: '确认取消订单?',
          success: res => {
            if (res.confirm) {
              cancelOrder({orderId: item.orderId, orderFrom: item.orderFrom}).then(response => {
                if (response.isError) return
                that.$utils.showToast({ title: '取消成功' })
                item.orderStatus = -1
              })
            }
          }
        })
      } else {
        checkCancelPaidOrder({orderId: item.orderId, orderFrom: item.orderFrom}).then(response => {
          if (response.isError) return
          this.$pt.navigateTo({url: '/pages/personal/order/cancel-order/main?memberId=' + item.memberId + '&orderId=' + item.orderId})
        })
      }
    },
    queryOrderInfo(order) {
      if (order.orderFrom === 1) {
        // 自营订单
        this.$pt.navigateTo({ url: '/pages/personal/order/detail/main?orderId=' + order.orderId + '&orderFrom=' + order.orderFrom })
      } else {
        // 分销订单
        this.$pt.navigateTo({ url: '/pages/personal/distribution/order/detail/main?orderId=' + order.orderId +
           '&personalType=0&orderFrom=' + order.orderFrom })
      }
    },
    // 买家
    pageBuyerOrder() {
      const params = {
        customerMemberId: this.getCustomerId,
        orderStatusInfo: '',
        currentPage: this.currentPage,
        keyword: this.keyword,
        size: 10
      }
      pageCustomerOrder(params).then((response) => {
        if (response.isError) return
        response.data.records.forEach(item => {
          item.moreGoods = false
          if (item.orderGoodsList.length > 2) {
            item.moreGoods = true
            item.orderGoodsList.length = 2
          }
          this.orders.push(item)
        })
        this.currentPage++
        if (this.orders.length === 0 || this.orders.length >= response.data.total) {
          this.pullLoad.finished = true
        }
      }).catch(() => {
      })
    },
    // 卖家自营
    pageSellSelfOrder() {
      const params = {
        memberId: this.getCustomerId,
        orderStatus: '',
        currentPage: this.currentPage,
        orderType: 10,
        keyword: this.keyword,
        size: 10
      }
      this.$pt.showLoading({
        title: '加载中',
        mask: true
      })
      pageUserOrder(params).then((response) => {
        this.$pt.hideLoading()
        if (response.isError) return
        this.currentPage++
        response.data.records.forEach(item => {
          this.orders.push(item)
        })
        if (this.orders.length === 0 || this.orders.length >= response.data.total) {
          this.pullLoad.finished = true
        }
      }).catch(() => {
      })
    },
    // 卖家分销
    pageProjectOrderByMemberId() {
      const params = {
        memberId: this.getCustomerId,
        orderStatusList: '',
        currentPage: this.currentPage,
        keyword: this.keyword,
        size: 10
      }
      pageProjectOrderByMemberId(params).then((response) => {
        if (response.isError) return
        response.data.records.forEach(item => {
          item.moreGoods = false
          if (item.orderGoodsList.length > 2) {
            item.moreGoods = true
            item.orderGoodsList.length = 2
          }
          this.orders.push(item)
        })
        this.currentPage++
        if (this.orders.length === 0 || this.orders.length >= response.data.total) {
          this.pullLoad.finished = true
        }
      }).catch(() => {
      })
    },
    getOrderDic() {
      getDict('UserOrderDTO.orderStatus').then((response) => {
        if (response.isError) return
        this.orderStatusMap = response.data.orderStatus
      }).catch(() => {
      })
    },
    resetData() {
      // 重置数据
      this.searchNull = false
      this.currentPage = 1
      this.orders = []
      this.pullLoad.finished = false
    },
    onSearch(e) {
      // 键盘直接搜索点击事件
      this.keyword = e.mp.detail
      this.resetData()
      switch (this.getCurrentPage().query.searchType) {
        case 'buyer':
          // 买家
          this.pageBuyerOrder()
          break
        case 'seller-self':
          // 卖家自营
          this.pageSellSelfOrder()
          break
        case 'seller-other':
          // 卖家分销
          this.pageProjectOrderByMemberId()
          break
      }
    },
    onChangeVal(e) {
      this.keyword = e.mp.detail
    },
    onSearch2() {
      // 搜索按钮点击事件
      this.resetData()
      switch (this.getCurrentPage().query.searchType) {
        case 'buyer':
          // 买家
          this.pageBuyerOrder()
          break
        case 'seller-self':
          // 卖家自营
          this.pageSellSelfOrder()
          break
        case 'seller-other':
          // 卖家分销
          this.pageProjectOrderByMemberId()
          break
      }
    },
    pageTo(item) {
      // 跳转详情页
      this.$pt.navigateTo({url: '/pages/shop/detail/main?goodsId=' + item.goodsId + '&goodsType=' + item.goodsType})
    }
  }
}
</script>
<style lang="less" scoped>
.container {
  min-height: 100vh;
}
.tag-win{
    background: #fbf3d4;
    color: #333333;
    padding: 2px 6px;
    border-radius: 5px;
  }
  .order-div {
    background-color: #fff;
    border-radius: 5px;
    margin: 10px;
    font-size: 13px;
    padding: 10px;
  }

  .userinfo-avatar {
    width: 20px;
    height: 20px;
    border-radius: 50%;
    margin-right: 6px;
  }

  .goods-title {
    height: 40px;
    overflow: hidden;
  }

  .img-item {
    width: 70px;
    height: 70px;
    border-radius: 2px;
  }

  .order-btn {
    background-color: #fff;
    color: #969896;
    font-size: 12px;
    line-height: 1;
    display: inline-block;
    padding: 6px;
    border-radius: 5px;
    border: #ededed 1px solid!important;
  }

  .btn-save {
    background-color: #f8c400;
  }

  .btn-border {
    border: 1px solid #969896;
    border-radius: 5px;
    height: 20px;
    background-color: #ffffff;
  }
.search-box {
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background: white;
  z-index: 9;
  // 对搜索框进行改造
  /deep/ .van-search--show-action {
    background: unset !important;
    padding-right: unset;
  }
  /deep/ .van-cell {
    background: #f4f4f4;
    border-radius: 25px;
  }
  /deep/ .van-search__action {
    background: white;
    font-size: 20px;
    color: #333333;
    font-weight: bold;
  }
}
.main {
  .main-bar {
    position: relative;
    padding: 0 15px 20px;
    margin-top: 10px;    
  }
}
.type-0{
  color: #f94035;
}
.type-1{
  color: #ff6600;
}  
.type-2{
  color: #409eff;
}
.type-3{
  color: #ff6600;
}
.type-4{
  color: #67c23a;
}
.type--10, .type--11{
  color: #ff6600;
}
.type--1{
  color: #000;
}
.border-b{
  border-bottom: #e4e4e4 1px solid;
}
.barbg{
  background: #fafafa;
}
.orders-border{
  position: relative;
  &:after{
    content: '';
    display: block;
    position: absolute;
    left: 0;
    bottom: 0;
    width: 100%;
    height: 1px;
    background: #e6e6e6;
  }
  &:last-child{
    &:after{
      display: none;
    }
  }
}
.goods-prop{
  box-sizing: border-box;
  height: 27px;
  line-height: 1.3;
  overflow:hidden;
  text-overflow:ellipsis;
  display:-webkit-box;
  -webkit-line-clamp:1;
  -webkit-box-orient:vertical;
  word-break:break-word;
}
.order-div {
  background-color: #fff;
  border-radius: 5px;
  font-size: 13px;
}
.goods-title {
  height: 42px;
  font-size: 15px;
  line-height: 1.4;
  overflow:hidden;
  text-overflow:ellipsis;
  display:-webkit-box;
  -webkit-line-clamp:2;
  -webkit-box-orient:vertical;
  word-break:break-word;
}

.img-item {
  width: 100px;
  height: 100px;
}

.order-btn {
  background-color: #fff;
  color: #999999;
  font-size: 12px;
  line-height: 1;
  display: inline-block;
  padding: 6px 12px;
  border-radius: 50px;
  border: #cccccc 1px solid!important;
}
.order-btn2 {
  border: none!important;
  background: linear-gradient(to right, #fed801, #fcc508);
  background-color: #fff;
  color: #000;
  font-size: 12px;
  line-height: 1;
  display: inline-block;
  padding: 6px 12px;
  border-radius: 50px;
}

.btn-save {
  background-color: #f8c400;
}

.btn-border {
  border: 1px solid #969896;
  border-radius: 5px;
  height: 20px;
  background-color: #ffffff;
}
.nodata {
  margin-top: 135px;
}
.nodata {
  margin-top: 115px;
}
</style>
